import * as THREE from 'three';

export class Explosion {
    private particleSystem: THREE.Points;
    private geometry: THREE.BufferGeometry;
    private material: THREE.PointsMaterial;

    constructor(scene: THREE.Scene, position: THREE.Vector3) {
        this.geometry = new THREE.BufferGeometry();
        // 创建粒子数据
        const positions = new Float32Array(100 * 3);
        const colors = new Float32Array(100 * 3);

        for (let i = 0; i < 100; i++) {
            const i3 = i * 3;
            positions[i3] = (Math.random() - 0.5) * 2;
            positions[i3 + 1] = (Math.random() - 0.5) * 2;
            positions[i3 + 2] = (Math.random() - 0.5) * 2;

            colors[i3] = Math.random();
            colors[i3 + 1] = Math.random();
            colors[i3 + 2] = Math.random();
        }

        this.geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
        this.geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3));

        this.material = new THREE.PointsMaterial({
            size: 0.1,
            vertexColors: true,
            transparent: true,
            opacity: 0.8,
            // 确保不参与物理碰撞
            depthTest: false, // 避免深度测试干扰
        });

        this.particleSystem = new THREE.Points(this.geometry, this.material);
        this.particleSystem.position.copy(position);
        scene.add(this.particleSystem);

        // 不添加到物理世界，避免碰撞
    }

    public update(deltaTime: number): void {
        // 更新粒子系统
        // 例如，改变粒子的颜色、位置等
    }

    public dispose(): void {
        // 从场景中移除粒子系统
        this.particleSystem.parent?.remove(this.particleSystem);

        // 清理资源
        this.geometry.dispose();
        this.material.dispose();
    }
} 
